package com.brotherly.util;

public class ByteOrder
{
    public static byte ring_shift_left(byte value, int bits)
    {
        bits = bits % Byte.SIZE;
        return (byte) ((value << bits) | (value >>> (Byte.SIZE - bits)));
    }

    public static short ring_shift_left(short value, int bits)
    {
        bits = bits % Short.SIZE;
        return (short) ((value << bits) | (value >>> (Short.SIZE - bits)));
    }

    public static int ring_shift_left(int value, int bits)
    {
        bits = bits % Integer.SIZE;
        return (int) ((value << bits) | (value >>> (Integer.SIZE - bits)));
    }

    public static long ring_shift_left(long value, int bits)
    {
        bits = bits % Long.SIZE;
        return (long) ((value << bits) | (value >>> (Long.SIZE - bits)));
    }

    public static byte ring_shift_right(byte value, int bits)
    {
        bits = bits % Byte.SIZE;
        return (byte) ((value >>> bits) | (value << (Byte.SIZE - bits)));
    }

    public static short ring_shift_right(short value, int bits)
    {
        bits = bits % Short.SIZE;
        return (short) ((value >>> bits) | (value << (Short.SIZE - bits)));
    }

    public static int ring_shift_right(int value, int bits)
    {
        bits = bits % Integer.SIZE;
        return (int) ((value >>> bits) | (value << (Integer.SIZE - bits)));
    }

    public static long ring_shift_right(long value, int bits)
    {
        bits = bits % Long.SIZE;
        return (long) ((value >>> bits) | (value << (Long.SIZE - bits)));
    }

    public static short swap(short value)
    {
        return (short) ((value >>> 8) & 0x00ff | (value & 0x00ff) << 8);
    }

    public static int swap(int value)
    {
        return ring_shift_right(value & 0x00ff00ff, 8) | (ring_shift_left(value, 8) & 0x00ff00ff);
    }

    public static long swap(long value)
    {
        return ((long) swap((int) (value & 0xffffffff))) << 32 | swap((int) (value >> 32));
    }
}
